package pixel

import(
	"github.com/go-gl/gl/v2.1/gl"
	"os"
	"log"
	_"image/jpeg"
	_"image/png"
	"image/draw"
	"image"
)

func MakeTexture(file string) uint32{
	imgFile, err := os.Open(file)
	if err != nil {
		log.Fatalf("texture %q not found on disk: %v\n", file, err)
	}
	defer imgFile.Close()
	img,_,err := image.Decode(imgFile)
	if err != nil {
		panic(err)
	}
	rgba := image.NewRGBA(img.Bounds())
	if rgba.Stride != rgba.Rect.Size().X*4 {
		panic("unsupported stride")
	}
	draw.Draw(rgba, rgba.Bounds(), img, rgba.Bounds().Min, draw.Src)
	var texture uint32
	//创建id为1的纹理
	gl.Enable(gl.TEXTURE_2D)
	//gl.Enable(gl.TEXTURE_GEN_S)
	//gl.Enable(gl.TEXTURE_GEN_T)
	gl.GenTextures(1,&texture)
	//绑定纹理到2d
	gl.BindTexture(gl.TEXTURE_2D,texture)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		int32(rgba.Rect.Size().X),
		int32(rgba.Rect.Size().Y),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(rgba.Pix))

	return texture
}